package com.yuan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuan.reggie.common.BaseContext;
import com.yuan.reggie.common.CustomerException;
import com.yuan.reggie.common.R;
import com.yuan.reggie.pojo.ShoppingCart;
import com.yuan.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yuanxingke
 * @date 2022/8/3 23:16
 */
@RestController
@Slf4j
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){

        //1、查询购物车数据是否存在
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        shoppingCartQueryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(shoppingCartQueryWrapper);

        if(cart!=null){
            //不等于null 说明以前就有此 所以在原有的基础上加一
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }


    @GetMapping("list")
    public R<List<ShoppingCart>> list(){

        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartQueryWrapper.orderByAsc("create_time");
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartQueryWrapper);
        return R.success(shoppingCarts);
    }

    @DeleteMapping("clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){


        //1、查询购物车数据是否存在
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if(cart!=null){
            //不等于null 说明以前就有此 所以在原有的基础上减一
            if (cart.getNumber()==1) {
                QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
                shoppingCartQueryWrapper.eq("user_id",BaseContext.getCurrentId());
//                shoppingCartQueryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
//                shoppingCartQueryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
                shoppingCartQueryWrapper.eq("id",cart.getId());
                shoppingCartService.remove(shoppingCartQueryWrapper);
            }else if(cart.getNumber()>1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartService.updateById(cart);
            }else{
                throw new CustomerException("不能为0");
            }

        }
        return R.success("减成功");
    }
}
